import 'dart:io';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:note/models/appSetModel.dart';
import 'package:note/models/noteModel.dart';
import 'package:path_provider/path_provider.dart';

/// Hive 数据操作
class HiveUtil {
  /// 实例
  static HiveUtil? instance;

  HiveUtil._();

  ///box
  //笔记box
  static late Box noteBox;
  //设置信息box
  static late Box appSetBox;

  /// 初始化，需要在 main.dart 调用
  /// <https://docs.hivedb.dev/>
  static Future<void> install() async {
    /// 初始化数据库地址
    Directory document = await getApplicationDocumentsDirectory();
    Hive.init(document.path);

    /// 注册自定义对象（实体）
    /// <https://docs.hivedb.dev/#/custom-objects/type_adapters>
    /// Hive.registerAdapter(SettingsAdapter());
    Hive.registerAdapter(NoteAdapter());
    Hive.registerAdapter(AppSetModelAdapter());
  }

  /// 初始化 Box
  static Future<HiveUtil> getInstance() async {
    if (instance == null) {
      instance = HiveUtil._();
      await Hive.initFlutter();

      //笔记
      noteBox = await Hive.openBox('noteBox');
      //app设置
      appSetBox = await Hive.openBox('appSetBox');
    }
    return instance!;
  }
}
